package com.user.write.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import com.user.write.dto.UserDTO;
import com.user.write.service.UserService;
import com.user.write.vo.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserReadController
 * @Desecription TODO 1.2新增
 * @Author gjy
 * @Date 2021/3/19 12:42
 * @Version 1.2
 **/
@RefreshScope
@RestController
@RequestMapping("/user")
public class UserWriteController {

    private static final Logger log = LoggerFactory.getLogger(UserWriteController.class);

    BloomFilter<CharSequence> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8),
            20000, 1E-7);
    @Autowired
    private UserService userService;

    @DeleteMapping("/delete")
    public void get(@RequestParam("id") int id) {
        userService.deleteOne(id);
    }

    @PutMapping("/addOne")
    public ResultData addOne(@RequestBody UserDTO userDTO) {
        ResultData resultData = new ResultData("1", "请求成功");
        try {
            int i = userService.addOne(userDTO);
            //int i = 1/0;
            resultData.setData(i);
        } catch (Exception e) {
            e.printStackTrace();
            resultData.setMessage("请求失败");
            resultData.setCode("-1");
            return resultData;
        }
        return resultData;
    }

    @PostMapping("/UpdateOne")
    public ResultData updateOne(@RequestBody UserDTO userDTO) {
        ResultData resultData = new ResultData("1", "请求成功");
        try {
            userService.updateOne(userDTO);
            //int i = 1/0;
        } catch (Exception e) {
            e.printStackTrace();
            resultData.setMessage("请求失败");
            resultData.setCode("-1");
            return resultData;
        }
        return resultData;
    }

    @PostMapping("/excel/import")
    public ResultData upload(@RequestParam("file") MultipartFile file) throws IOException {
        ResultData resultData = new ResultData("1", "导入成功");
        final List<UserDTO> list = new ArrayList<UserDTO>();
        long start = System.currentTimeMillis();
        EasyExcel.read(file.getInputStream(), UserDTO.class, new AnalysisEventListener<UserDTO>() {
            public void invoke(UserDTO userDTO, AnalysisContext context) {
                //以学生姓名作为判重条件，判断布隆过滤器里是否存在这个学生姓名，不存在则添加到布隆过滤器中
                if (!bloomFilter.mightContain(userDTO.getStuName())){
                    bloomFilter.put(userDTO.getStuName());
                    list.add(userDTO);
                }
            }
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("读取完成");
                System.out.println(list.size());
                for (UserDTO userDTO : list) {
                    System.out.println(userDTO);
                }
            }
        }).sheet().doRead();
        /*userService.addMany(list);*/
        long end = System.currentTimeMillis();

        log.info("导入耗时{}",end-start);
        return resultData;
    }
}
